﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using DevExpress.XtraCharts;

namespace Lamex.mSchedule.UI
{
    public partial class frmSCH_Owed_Analyze_Overall : frmBaseChild
    {
        public frmSCH_Owed_Analyze_Overall()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmSCH_Owed_Analyzation_Load);
        }

        private void frmSCH_Owed_Analyzation_Resize(object sender, EventArgs e)
        {
            panel1.Height = this.Height / 2;
            panel2.Height = this.Height / 2;

            gcSalesOrg.Width = panel1.Width / 4;
            gcOrderType.Width = panel1.Width / 4;
            gcMRPControl.Width = panel1.Width / 4;
            gcProductAssign.Width = panel1.Width - panel1.Width / 4 * 3;
        }

        private void frmSCH_Owed_Analyzation_Load(object sender,EventArgs e)
        {
            setData_SalesOrg(chrtBy_SalesOrg, ViewType.Pie);

            setData_Others(chrtby_OrderType, "Order_Type", ViewType.Point);
            setData_Others(chrtBy_MRPControl, "MRP_Owner", ViewType.Bar);
            setData_Others(chrtBy_ProductAssign, "Prod_Category", ViewType.Line);

            setData_WorkCenter(chrtBy_WorkCenter, ViewType.Bar);
        }

        #region 图表数据加载

        private void setData_SalesOrg(ChartControl chart, ViewType vt)
        {
            string sql = @"SELECT SalesOrg,SUM(OWED_QTY) OWED_QTY
FROM View_OP_Follow_Owed_QTY
GROUP BY SalesOrg
ORDER BY OWED_QTY DESC";

            DataTable dt1 = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            if (dt1.Rows.Count != 0)
            {
                chart.Series.Clear();
                //Series seriesAuto = new Series("", ViewType.Pie);
                Series seriesAuto = new Series("", vt);
                string dt1Sum = dt1.Compute("sum(OWED_QTY)", "true").ToString();

                foreach (DataRow dr in dt1.Rows)
                {
                    //dr["Title"] = percent;
                    string StatusName = "";
                    SeriesPoint p = new SeriesPoint();

                    //原值显示
                    double[] d = new double[] { double.Parse(Math.Round((decimal.Parse(dr["OWED_QTY"].ToString())), 0).ToString()) };
                    p.Values = d;
                    if (dr["SalesOrg"].ToString() == "")
                    {
                        p.Argument = "NULL";
                    }
                    else
                    {
                        p.Argument = dr["SalesOrg"].ToString();
                    }

                    //百分比显示
                    //double[] d = new double[] { double.Parse(Math.Round((decimal.Parse(dr["OWED_QTY"].ToString()) / decimal.Parse(dt1Sum)) * 100, 2).ToString()) };
                    //p.Values = d;
                    //string percent = dr["SalesOrg"].ToString() + "(" + Math.Round((decimal.Parse(dr["OWED_QTY"].ToString()) / decimal.Parse(dt1Sum)) * 100, 2).ToString() + "%" + ")";
                    //p.Argument = percent;

                    //设置Series的标签
                    seriesAuto.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//每个点都显示数据
                    seriesAuto.Points.Add(p);
                }

                //seriesAuto.DataSource = dtProcess;
                //seriesAuto.ArgumentScaleType = ScaleType.Qualitative;
                ////名称
                //seriesAuto.ArgumentDataMember = "BARCODE";
                ////名称
                //seriesAuto.ArgumentDataMember = "Title";
                //seriesAuto.ValueScaleType = ScaleType.Numerical;

                ////取值字段 
                //seriesAuto.ValueDataMembers.AddRange(new string[] { "iSum" });
                //(seriesAuto.Label as PieSeriesLabel).Position = PieSeriesLabelPosition.Outside;
                //seriesAuto.Label.TextPattern = "as";

                //显示标项目名称和百分比数据
                //seriesAuto.Visible = false;
                seriesAuto.Label.Font = new System.Drawing.Font("Tahoma", 8F);
                seriesAuto.PointOptions.PointView = PointView.ArgumentAndValues;//属性已过时
                //seriesAuto.Label.TextPattern = "{A}: {VP:P2}";
                //seriesAuto.LegendTextPattern = "{A}: {V}:{VP}";
                //seriesAuto.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;

                //(seriesAuto.Label as PieSeriesLabel).Position = PieSeriesLabelPosition.Outside;

                //_pieView.RuntimeExploding = false;

                //设置Series的标签
                //seriesAuto.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//每个点都显示数据

                //ChartTitle autoTitle = new ChartTitle();
                //autoTitle.Text = "Hour";
                //chartHour.Titles.Clear();
                //chartHour.Titles.Add(autoTitle);

                chart.Series.Add(seriesAuto);
                chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
                Legend Legends = (Legend)chart.Legend;
                Legends.Visibility = DevExpress.Utils.DefaultBoolean.False;
                Legends.AlignmentVertical = LegendAlignmentVertical.Top;
                Legends.AlignmentHorizontal = LegendAlignmentHorizontal.Left;
                Legends.TextVisible = true;
                //PieSeriesView _pieView = (PieSeriesView)seriesAuto.View;
                //_pieView.ExplodeMode = PieExplodeMode.All;
                //_pieView.ExplodedDistancePercentage = 50;


            }
        }

        private void setData_Others(ChartControl chart, string feild, ViewType vt)
        {
            string sql = @"SELECT " + feild + @",SUM(OWED_QTY) OWED_QTY
FROM View_OP_Follow_Owed_QTY
GROUP BY " + feild + @"
ORDER BY OWED_QTY DESC";

            DataTable dt1 = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            if (dt1.Rows.Count != 0)
            {
                chart.Series.Clear();
                Series seriesAuto = new Series("", vt);

                foreach (DataRow dr in dt1.Rows)
                {
                    SeriesPoint p = new SeriesPoint();

                    //原值显示
                    if (dr[feild].ToString() != "")
                    {
                        double[] d = new double[] { double.Parse(Math.Round((decimal.Parse(dr["OWED_QTY"].ToString())), 0).ToString()) };
                        p.Values = d;
                        p.Argument = dr[feild].ToString();
                        seriesAuto.Points.Add(p);
                    }
                }

                chart.Series.Add(seriesAuto);
                chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
                Legend Legends = (Legend)chart.Legend;
                Legends.Visibility = DevExpress.Utils.DefaultBoolean.True;
                Legends.AlignmentVertical = LegendAlignmentVertical.Top;
                Legends.AlignmentHorizontal = LegendAlignmentHorizontal.Left;
                Legends.TextVisible = true;

                //强制显示所有x轴标签
                XYDiagram diagram = ((XYDiagram)chart.Diagram);
                diagram.AxisX.QualitativeScaleOptions.AutoGrid = false;
                diagram.AxisX.Label.ResolveOverlappingOptions.AllowHide = false;

                //设置Series的标签
                seriesAuto.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//每个点都显示数据
                if (vt.Equals(ViewType.Bar))
                {
                    //设置标签的显示位置，以下报错
                    SideBySideBarSeriesLabel label = (SideBySideBarSeriesLabel)seriesAuto.Label;
                    label.Position = BarSeriesLabelPosition.Top;
                }
                //seriesAuto.Label.TextAlignment = StringAlignment.Near;//不起作用
            }
        }

        private void setData_WorkCenter(ChartControl chart, ViewType vt)
        {
            string sql = @"  SELECT PROCESS_CODE, PROCESS_NAME, SUM(PROCESS_TIME)/3600 AS PROCESS_TIME
  FROM View_OP_Follow_Process_Time
  GROUP BY PROCESS_CODE,PROCESS_NAME
  ORDER BY PROCESS_CODE";

            DataTable dt1 = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql);
            if (dt1.Rows.Count != 0)
            {
                chart.Series.Clear();
                Series seriesAuto = new Series("", vt);

                foreach (DataRow dr in dt1.Rows)
                {
                    SeriesPoint p = new SeriesPoint();

                    //原值显示
                    if (dr["PROCESS_CODE"].ToString() != "")
                    {
                        double[] d = new double[] { double.Parse(Math.Round((decimal.Parse(dr["PROCESS_TIME"].ToString())), 0).ToString()) };
                        p.Values = d;
                        p.Argument = dr["PROCESS_CODE"].ToString();
                        seriesAuto.Points.Add(p);
                    }
                }

                chart.Series.Add(seriesAuto);

                chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
                Legend Legends = (Legend)chart.Legend;
                Legends.Visibility = DevExpress.Utils.DefaultBoolean.True;
                Legends.AlignmentVertical = LegendAlignmentVertical.Top;
                Legends.AlignmentHorizontal = LegendAlignmentHorizontal.Left;
                Legends.TextVisible = true;

                //强制显示所有x轴标签
                XYDiagram diagram = ((XYDiagram)chart.Diagram);
                diagram.AxisX.QualitativeScaleOptions.AutoGrid = false;
                diagram.AxisX.Label.ResolveOverlappingOptions.AllowHide = false;

                //设置Series的标签
                seriesAuto.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//每个点都显示数据
                //设置标签的显示位置
                SideBySideBarSeriesLabel label = (SideBySideBarSeriesLabel)seriesAuto.Label;
                label.Position = BarSeriesLabelPosition.Top;
            }
        }

        #endregion
    }
}